<?php

use console\db\Migration;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;

/**
 * 系统数据库迁移升级准备
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300000_000000_init extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {

        /**
         * 首先复制用户表到新数据库
         * @create table `sns_dynamic_comment`.
         */
        $requireTables = $this->requiredTransferTable();
        if (!empty($requireTables)) {
            foreach ($requireTables as $table) {
                if (!$this->isExistsTable($table)) {
                    Console::error('Please migrate the original table: ' . $table . ' using the database tool first.');
                    return false;
                }
            }
        }

        /**
         * 创建升级所需的临时表
         */
        foreach ($this->tempTable() as $table) {
            $this->tableBuilder($table);
        }

        /**
         * 升级所需增加的表字段
         */
        foreach ($this->tempAddColumns() as $table => $columns) {
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            $tableColumns = $this->db->getTableSchema($tableName)->columnNames;
            if (empty($columns)) {
                continue;
            }
            foreach ($columns as $column => $type) {
                if (!in_array($column, $tableColumns)) {
                    $this->addColumn($tableName, $column, $type);
                } else {
                    $this->alterColumn($tableName, $column, $type);
                }
            }
        }

        /**
         * 升级所需增加的表索引
         */
        foreach ($this->tempAddIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->createIndex($index, $tableName, $index);
            }
        }

        /**
         * 升级所需临时移除的表索引
         */
        foreach ($this->tempRemoveIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->dropIndex($index, $tableName);
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        
    }

    /**
     * 迁移前所需的数据表
     * @todo 复制所需数据表至新数据库
     * @return array
     */
    public function requiredTransferTable() {
        return [
            '{{%member}}'
        ];
    }

    /**
     * 升级所需的临时表
     * @return array
     */
    public function tempTable() {
        return [
            [
                'name' => 'sns_dynamic_comment',
                'comment' => '动态评论表',
                'columns' => [
                    'id' => $this->primaryKey(),
                    'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                    'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                    'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                    'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                    'post' => $this->text()->notNull()->comment('评论内容'),
                    'author' => $this->string(50)->defaultValue('')->comment('作者'),
                    'listener' => $this->string(50)->defaultValue('')->comment('被回复人'),
                    'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                    'ts_comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('原泰山号动态评论ID'),
                    'status' => $this->tinyint(1)->notNull()->defaultValue(10),
                    'created_at' => $this->unixTimestamp()->comment('创建时间'),
                    'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                    'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
                ],
                'index' => [
                    [
                        'columns' => 'dynamic_id',
                    ],
                    [
                        'columns' => 'comment_id',
                    ],
                    [
                        'columns' => 'reply_id',
                    ],
                    [
                        'columns' => 'user_id',
                    ]
                ]
            ]
        ];
    }

    /**
     * 升级所需增加的表字段
     * @return array
     */
    public function tempAddColumns() {
        return [];
    }

    /**
     * 升级所需增加的临时索引
     * @return array
     */
    public function tempAddIndexs() {
        return [];
    }

    /**
     * 升级所需临时移除的索引
     * @return array
     */
    public function tempRemoveIndexs() {
        return [];
    }

}
